package com.isoftstone.bank.controller;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.bank.service.impl.BOrgService;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.common.service.impl.SysUserService;
import com.isoftstone.merchant.entity.notify.BNotify;
import com.isoftstone.merchant.entity.notify.BNotifyDetail;
import com.isoftstone.merchant.service.notice.INotifyService;
import com.isoftstone.util.Const;
import com.isoftstone.util.JsonUtil;
import com.isoftstone.util.PageData;
import com.isoftstone.util.TreeNode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by wlj on 2017/12/14
 * 通知管理
 */
@Controller
public class BankNotifyController extends BaseController{
    @Autowired
    private BOrgService orgService;
    @Autowired
    private SysUserService userService;
    @Autowired
    INotifyService notifyService;
    /*
    * 到通知列表
    * */
    @RequiresPermissions("bms-menu.004.001")
    @RequestMapping("bank/notify/getNotifyList")
    public ModelAndView getNotifyList(Page page){
        ModelAndView md=new ModelAndView();
        md.setViewName("bank/notice/notice_manage");
        return  md;
    }
    /*
    * 到通知添加页面
    * */
    @RequestMapping("bank/notify/addNotify")
    public ModelAndView addNotify(){
        ModelAndView md=new ModelAndView();
        md.setViewName("bank/notice/add_notice");
        return md;
    }
    /*
    * 到通知详情页面
    * */
    @RequestMapping("bank/notify/notifyInfo")
    public ModelAndView notifyInfo(){
        ModelAndView md=new ModelAndView();

        PageData pd = this.getPageData();

        md.addObject("NOTICE", JsonUtil.beanToJson(pd));
        md.setViewName("bank/notice/notice_info");
        return md;
    }
    /*
    * 新增通知
    * */
    @RequestMapping(value = "bank/notify/insertNotify",method = RequestMethod.POST)
    @ResponseBody
    //前台传入treeDemoList(集合),notifyContext
    public Object insertNotify(){
        Tidings<String> tidings = new Tidings<String>();
        PageData pd = this.getPageData();

            BNotify bNotify = new BNotify();
            bNotify.setNotifyContext(pd.get("desc")+"");
//            bNotify.setNotifyType("0001");
            bNotify.setPhoneType("0001");
            bNotify.setCreatorType("01");
            bNotify.setStatus("1");
            bNotify.setCreatorUnit(this.getUser().getCorpId());//银行人员：存机构
            bNotify.setCreateDate(new Date());
            bNotify.setCreator(this.getUser().getUserid());//当前登录用户

            BNotifyDetail bNotifyDetail = new BNotifyDetail();
            bNotifyDetail.setIs_read("0");//新增时设置消息未读
            bNotifyDetail.setUser_type("02");

        Object targetType = pd.get("targetType");
        if(null != targetType && targetType.equals(Const.NotifyTarget.ZERO)){//全部
            String [] targetList = this.userService.getMerchantUserListByCorpIds(pd);
            String [] targetListFee = this.userService.getFeeUserListByCorpIds(pd);

            BNotifyDetail bNotifyDetailFee = new BNotifyDetail();
            bNotifyDetailFee.setIs_read("0");//新增时设置消息未读
            bNotifyDetailFee.setUser_type("03");
//            notifyService.insertNotify(targetList,bNotifyDetail,targetListFee,bNotifyDetailFee,bNotify); //存缴费人员
            bNotify.setNotifyType("03");
            notifyService.insertNotify(targetList,bNotifyDetail,null,null,bNotify); //存商户人员
            bNotify.setNotifyType("04");
            notifyService.insertNotify(null,null,targetListFee,bNotifyDetailFee,bNotify); //存缴费人员
        }else if(null != targetType && targetType.equals(Const.NotifyTarget.THREE)){//商户端人员
            bNotify.setNotifyType("03");
            String [] targetList = this.userService.getMerchantUserListByCorpIds(pd);
            notifyService.insertNotify(targetList,bNotifyDetail,null,null,bNotify); //存商户人员
        }else if(null != targetType && targetType.equals(Const.NotifyTarget.FOUR)){//缴费用户
            bNotify.setNotifyType("04");
            bNotifyDetail.setUser_type("03");
            String [] targetList = this.userService.getFeeUserListByCorpIds(pd);
            notifyService.insertNotify(null,null,targetList,bNotifyDetail,bNotify); //存缴费人员
        }
        tidings.setStatus(Const.SUCCESS);
        tidings.setMsg("新增成功！");

        return tidings;
    }

    /*
    * 撤回(1-正常 0-撤回)
    * */
    @RequestMapping(value = "bank/notify/withdrawNotify",method = RequestMethod.POST)
    @ResponseBody
    public Object  withdrawNotify(Long notifySeq){
        System.out.println("notifySeq = " + notifySeq);
        Tidings<String> tidings = new Tidings<String>();
        try{
            notifyService.withdrawNotify(notifySeq);
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("撤回成功！");
        }catch(Exception e){
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("撤回失败！");
            e.printStackTrace();
        }
        return tidings;
    }

    /*
    * 通知列表
    * */
    @RequestMapping(value = "bank/notify/findnotifyinfoList" ,method = RequestMethod.POST)
    @ResponseBody
    public Object findnotifyList(Page page){
        PageData pd = this.getPageData();
        Tidings<List<BNotify>> listTiding = new  Tidings<List<BNotify>> ();
        List<TreeNode> orgList = orgService.selectOrgTreeNodeByOrgId(Long.valueOf(this.getUser().getCorpId()));
        String creatorUnit = "";
        if(null != orgList){
            String orgIds = this.getChildrenId(orgList);
            if(null != orgIds && !"".equals(orgIds)){
                String tem = orgIds.toString();
                if(",".equals(tem.substring(tem.length()-1,tem.length()))){
                    creatorUnit = tem.substring(0,tem.length()-1);
                }else{
                    creatorUnit = orgIds;
                }
            }
        }
        if(null !=creatorUnit){
            pd.put("creatorUnit", Arrays.asList(creatorUnit.split(",")));
        }
        page.setPd(pd);
        try {
            List<BNotify> bNotifies = notifyService.findnotifylistPage(page);
            listTiding.setT(bNotifies).setPage(page);
            listTiding.setStatus(Const.SUCCESS);
            //List<BNotify> bNotifyList =  notifyService.findnotifylis();
        } catch (Exception e) {
            listTiding.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return listTiding ;
    }

    private String getChildrenId(List<TreeNode> children){
        String str = "";
        if (children != null){
            for(int i=0;i<children.size();i++){
                TreeNode temNode = children.get(i);
                str = str + temNode.getId() +",";
                if(temNode.getChildren() != null ){
                    str = str + getChildrenId(temNode.getChildren()) ;
                }
            }
        }
        return str;
    }
}
